#_*_coding:utf-8_*_
import  time,unittest,HTMLTestRunner
from Common.function import project_path
from Common.exceldata import read_excel
from Common.function import config_url
from PageObject.login_page import LoginPage
#from PageObject.login_page_update import LoginPage
from PageObject.index_page import IndexPage
from PageObject.add_position_page import AddPositionPage
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

class login_test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        excel_dir = os.path.join(project_path(), "Data")
        excel_path = os.path.join(excel_dir, "testdata.xlsx")

        # 自动创建目录（可选）
        os.makedirs(excel_dir, exist_ok=True)

        # 读取 Excel 文件
        cls.data = read_excel(excel_path, 0)
        cls.driver = webdriver.Chrome()
        cls.driver.get(config_url())
        cls.driver.maximize_window()

    def test_01(self):
        login = LoginPage(self.driver)
        res = login.login_system("admin","admin123")
        self.assertIn("",res)

    def test_02(self):
        index = IndexPage(self.driver)
        res = index.position_menu()
        self.assertIn("index", res)

    def test_03(self):
        add = AddPositionPage(self.driver)
        res = add.add_post(self.data.get(1)[0],self.data.get(1)[1])
        self.assertIn("", "")
    
    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()




if __name__ == '__main__':
    import os  # 确保导入 os 模块

    suiteTest = unittest.TestSuite()
    suiteTest.addTest(login_test("test_01"))
    suiteTest.addTest(login_test("test_02"))
    suiteTest.addTest(login_test("test_03"))

    # 修正路径拼接并创建目录
    report_dir = os.path.join(project_path(), "Reports")
    os.makedirs(report_dir, exist_ok=True)  # 自动创建目录
    filepath = os.path.join(report_dir, "report.html")

    fp = open(filepath, 'wb')
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='自动化测试报告', description="测试报告")
    runner.run(suiteTest)
    fp.close()
